import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home/Home.vue'
import Login from '../views/Login/Login.vue'
import Register from '../views/Login/Register.vue'
import Shop from '../views/Shop/Shop.vue'
import OrderConfirmation from '../views/OrderConfirmation/OrderConfirmation.vue'
import OrderConfirmations from '../views/OrderConfirmation/OrderConfirmations.vue'
import OrderList from '../views/OrderList/OrderList.vue'
import CartList from '../views/CartList/CartList.vue'
import My_Information from '../views/Myinformation/MyInformation.vue'
import cs from '../views/cs'
const routes = [
	{
		path: '/',
		name: 'Home',
		component: Home
	},
	{
		path: '/cs',
		name: 'cs',
		component: cs
	},
	{
		path: '/login',
		name: 'Login',
		component: Login
	},
	{
		path: '/register',
		name: 'Register',
		component: Register,
		beforeEnter(to, from, next) {
			const isLogin = localStorage.isLogin;
			if (isLogin) {
				next({ name: 'Home' });
			} else {
				next();
			}
		}
	},
	{
		path: '/shop/:id',
		name: 'Shop',
		component: Shop,
	},
	{
		path: '/OrderConfirmation/:id',
		name: 'OrderConfirmation',
		component: OrderConfirmation,
	},
	{
		path: '/OrderConfirmations',
		name: 'OrderConfirmations',
		component: OrderConfirmations,
	},
	{
		path: '/OrderList',
		name: 'OrderList',
		component: OrderList,
	},
	{
		path: '/CartList',
		name: 'CartList',
		component: CartList,
	},
	{
		path: '/My_Information',
		name: 'My_Information',
		component:My_Information,
	}
]

const router = createRouter({
	history: createWebHashHistory(),
	routes
})

router.beforeEach((to, from, next) => {
	// 如果要访问的路径不是Login 则强行跳转到Login
	// 定义一个变量用于存放当前登录状态
	const isLogin = localStorage.isLogin;
	if (isLogin || to.name === 'Login' || to.name === 'Register') {
		next()
	} else {
		next({ name: 'Login' })
	}
})

export default router
